<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 15-1-10
 * Time: 下午5:18
 */
namespace Teacher;

class LessonStudentController extends \LessonStudentController
{
    public function index($lessonId)
    {
        $studentList = $this->getStudentListGroupByStatus($lessonId);

        return \Response::json($studentList);
    }

    protected function getStudentListGroupByStatus($lessonId)
    {
        $studentList['applyOk'] = \Lesson_Student::with('student')
            ->with('status')
            ->where('lesson_id', $lessonId)
            ->where('status_id', 1)
            ->get()->toArray();
        $studentList['applyIn'] = \Lesson_Student::with('student')
            ->with('status')
            ->where('lesson_id', $lessonId)
            ->where('status_id', 2)
            ->get()->toArray();
        $studentList['applyOut'] = \Lesson_Student::with('student')
            ->with('status')
            ->where('lesson_id', $lessonId)
            ->where('status_id', 3)
            ->get()->toArray();

        return $studentList;
    }

    public function update($lessonId, $studentId)
    {
        $status = \Input::get('status', 3);

        \Lesson_Student::where('lesson_id', $lessonId)
            ->where('student_id', $studentId)
            ->firstOrFail()
            ->update(['status_id' => $status]);

        return \Response::make(''. 200);
    }

    public function destroy($lessonId, $studentId)
    {
        $targetPivot = \Lesson_Student::where('lesson_id', $lessonId)
            ->where('student_id', $studentId)
            ->firstOrFail();

        $targetPivot->delete();

        return \Response::make('', 200);
    }


}